//
//  AppDelegate+UI.m
//  XingMei
//
//  Created by 钱金军 on 2019/3/20.
//  Copyright © 2019年 钱金军. All rights reserved.
//

#import "AppDelegate+UI.h"
#import "FilesHeader.h"
#import "IQKeyboardManager.h"
#import "AppDelegate+Service.h"
#import "DGTabBarViewController.h"

@implementation AppDelegate (UI)

- (void)setAppWindows{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //    [self configureAPIKey];//地图配置
    //    [self configurationLaunchUserOption]; //基本配置
    [self setNetWorkServerAndCdn];        //网络配置
    [self setRootViewController];         //设置根控制器
    [self autoKeyboard];                  //键盘自适应
    //    [self share];                         //分享
    //    [self baiduMapAPI];                    //百度地图配置
    //    [self resignWeichat];                  //注册微信
    //    [self checkUpdate];                    //检查更新
}

#pragma mark -- 设置根控制器
- (void)setRootViewController {
    BaseViewController *rootVc = [NSClassFromString(@"DGTabBarViewController") new];
    self.window.rootViewController = rootVc;
    
//    BaseViewController *rootVc = [NSClassFromString(@"LoginViewController") new];
//    self.window.rootViewController = rootVc;

    
    //    //是否登录
    //    if (![kUserDefaults boolForKey:firstLogin]) {
    
    //    }else{
    //        BaseViewController *rootVc = [NSClassFromString(@"DGTabBarViewController") new];
    //        self.window.rootViewController = rootVc;
    //    }
}

#pragma mark 键盘自动适应
-(void)autoKeyboard{
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
}

#pragma mark -- 获取当前视图根控制器
- (UIViewController *)getCurrentRootVC {
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    return result;
}

@end
